package com.yanqu.road.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.web.HttpGameWebParam;
import com.yanqu.road.server.helper.ErrorCodeHelper;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.utils.string.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bouncycastle.util.encoders.Base64;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

public class BaseGameHttpServlet extends BaseHttpServlet {

    private Logger logger = LogManager.getLogger(this.getClass().getName());

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        parseRequest(request);
    }

    /**
     * 返回解析后的参数对象
     * @param request
     * @return
     */
    public HttpGameWebParam parseRequest(HttpServletRequest request) {
        return parseRequestParamB64(request, false);
    }

    /**
     * 返回解析后的参数对象
     * @param request
     * @return
     */
    public HttpGameWebParam parseRequestParamB64(HttpServletRequest request, boolean paramB64) {
        HttpGameWebParam httpParseParam = new HttpGameWebParam();
        String param = request.getParameter("param") == null ? "" : request.getParameter("param");
        JSONObject paramObj;
        try {
            if (paramB64) {
                param = org.apache.commons.codec.binary.StringUtils.newStringUtf8(Base64.decode(param));
            }
            logger.info("param: {}", param);
            param = URLDecoder.decode(param, "UTF-8");
            paramObj = (JSONObject) JSONObject.parse(param);
            httpParseParam.setLanguage(StringUtils.autoToUtf8(paramObj.getString("language")));
            httpParseParam.setParam(param);
            httpParseParam.setParamObj(paramObj);
        } catch (Exception e) {
            logger.error("json格式出错！ json 字符串param为{}", param, e);
        }
        return httpParseParam;
    }

    protected JSONObject getResultObj(int ret, String language) {
        JSONObject resultObj = new JSONObject();
        resultObj.put("result", ret);
        if (0 == ret) {
            resultObj.put("msg", "Success");
        } else{
            resultObj.put("msg", MultipleLanguageMgr.getContent(ErrorCodeHelper.ERROR_CODE_PREFIX + ret, language));
        }
        return resultObj;
    }

    protected JSONObject getUrlEncodeResultObj(int ret, String language) {
        JSONObject resultObj = new JSONObject();
        resultObj.put("result", ret);
        String msg;
        if (0 == ret) {
            msg = "Success";
        } else{
            msg = MultipleLanguageMgr.getContent(ErrorCodeHelper.ERROR_CODE_PREFIX + ret, language);
        }
        try {
            msg = URLEncoder.encode(msg, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        resultObj.put("msg", msg);
        return resultObj;
    }
}
